<template>
    <div>
    <h1>信息新增</h1>
    <talble>
        <tr>
            <td>名称</td>
            <td><input type="text" v-model="data.hname"></td>
        </tr>
        <tr>
            <td>图片</td>
            <td>
                <input type="file" @change="upload">
                <img style="height: 100px;width: 100px;" :src="'https://localhost:7041'+data.himg">
            </td>
        </tr>
        <tr>
            <td>入住时间</td>
            <td><input type="date" v-model="data.htime"></td>
        </tr>
        <tr>
            <td>状态</td>
            <td>
                <input type="checkbox" v-model="data.sates">选中代表true
            </td>
        </tr>
        <tr>
            <td>价格</td>
            <td><input type="text" v-model="data.hprice"></td>
        </tr>
        <tr>
            <td>类型</td>
            <td>
                <select v-model="data.typeid">
                    <option value="0">请选择</option>
                    <option v-for="s in types" :value="s.typeid">{{s.typename}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="button" value="提交" @click="Add"></td>
        </tr>
    </talble>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref } from 'vue';
import { useRouter,useRoute} from 'vue-router';
import { onMounted } from 'vue';


const route=useRoute();
const router=useRouter();

onMounted(()=>
{
    Getdata();
    Getdataii()
})

const Getdata=()=>
{
    axios.get("https://localhost:7041/api/type/GetInterfaces",{params:types.value}).then(res=>
        {
            types.value=res.data;
        }
    )
}

const data=ref({
    "hid": 0,
  "hname": "",
  "himg": "",
  "htime": "",
  "sates": true,
  "hprice": 0,
  "typeid": 0
})
const types=ref([{
    "typeid": 0,
    "typename": ""
}])

const upload=(a:any)=>
{
  var f=a.target.files[0]
  var fd=new FormData();
  fd.append("file",f);

  axios.post("https://localhost:7041/api/upload/upload",fd).then(res=>
{
    data.value.himg=res.data;
}
  )
}

const Add=()=>
{
    axios.post("https://localhost:7041/api/hotel/update",data.value).then(res=>
        {
            if(res.data>0)
        {
            alert("修改成功");
            router.push({path:"/App"});
        }
        else{
            alert("修改失败");
        }
        }
    )
}


const Getdataii=()=>
{
    var _id=route.query.id
    axios.get("https://localhost:7041/api/hotel/fan",{params:{id:_id}}).then(res=>
        {
            data.value=res.data;
            data.value.htime=res.data.htime.substring(0,10);
          
        }
    )
}



</script>

<style scoped>

</style>